﻿Imports System.Drawing

Module Module1

    Sub Main()
        Dim bpa As Byte() = IO.File.ReadAllBytes("C:\Users\Evan\Desktop\sky\data\MAP_BG\d01p11b.bma")
        Dim bpc As Byte() = IO.File.ReadAllBytes("C:\Users\Evan\Desktop\sky\data\MAP_BG\d01p11b.bpc")
        Dim bpl As Byte() = IO.File.ReadAllBytes("C:\Users\Evan\Desktop\sky\data\MAP_BG\d01p11b.bpl")
        Dim bpaData As Byte() = GenericArrayOperations(Of Byte).CopyOfRange(bpa, &H2C, bpa.Length - 1)
        Dim bpcData As Byte() = GenericArrayOperations(Of Byte).CopyOfRange(bpc, &H10, bpa.Length - 1)
        Dim chunks As New List(Of Byte())
        For count As Integer = 0 To bpcData.Count - 1 Step 32
            chunks.Add(GenericArrayOperations(Of Byte).CopyOfRange(bpcData, count, count + 31))
        Next
        Dim i = ProcessMapping(bpaData, chunks, bpl)
        i.Save("C:\Users\Evan\Desktop\sky\data\MAP_BG\d01p11b.png")
    End Sub
    ''' <summary>
    ''' This is currently non-functional.  TODO: find size of output image, arrange tiles properly, find the appropriate palette index
    ''' </summary>
    ''' <param name="Data"></param>
    ''' <param name="Chunks"></param>
    ''' <param name="PalData"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function ProcessMapping(Data As Byte(), Chunks As List(Of Byte()), PalData As Byte()) As Bitmap
        Dim i As New System.Drawing.Bitmap(256, 192)
        Dim g As Graphics = Graphics.FromImage(i)
        Dim dataIndex As Integer = 0
        For y As Integer = 0 To 15
            For x As Integer = 0 To 3
                'If Chunks.Count > Data(dataIndex) Then
                If Chunks.Count > x + y * 9 Then
                    Dim icopy = ChunkToImage(Chunks(x + y * 9), PalData, 0)
                    ' Dim icopy = ChunkToImage(Chunks(Data(dataIndex)), PalData, 0)
                    g.DrawImage(icopy, New Point(x * 8, y * 8))
                    dataIndex += 1
                End If
                'End If
            Next
        Next
        Return i
    End Function
    Function Pen(Color As Byte, palData As Byte(), PalNumber As Byte) As Brush
        Dim r = palData(PalNumber * 64 + Color * 4 + 0)
        Dim g = palData(PalNumber * 64 + Color * 4 + 1)
        Dim b = palData(PalNumber * 64 + Color * 4 + 2)
        'Dim out As New SolidBrush(System.Drawing.Color.FromArgb(255, Color * 16, Color * 16, Color * 16))
        Dim out As New SolidBrush(System.Drawing.Color.FromArgb(255, r, g, b))
        Return out
    End Function

    Function ChunkToImage(Data As Byte(), PalData As Byte(), PalIndex As Byte) As Bitmap
        If Data.Length < 32 Then
            Return New Bitmap(8, 8)
            Exit Function
        End If
        Dim i As New System.Drawing.Bitmap(8, 8)
        Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(i)
        Dim colors As New List(Of Byte)
        For Each b In Data
            colors.Add(((b) Or &HF0) - &HF0)
            colors.Add(((b >> 4) Or &HF0) - &HF0)
        Next
        Dim colorIndex = 0
        For y As Byte = 0 To 7
            For x As Byte = 0 To 7
                g.FillRectangle(Pen(colors(colorIndex), PalData, PalIndex), x, y, 1, 1)
                colorIndex += 1
            Next
        Next
        g.Save()
        Return i
    End Function

    Public Class GenericArrayOperations(Of T)
        Public Shared Function CopyOfRange(ByteArr As T(), Index As Integer, EndPoint As Integer) As T()
            Dim output(Math.Max(Math.Min(EndPoint, ByteArr.Length) - Index, 0)) As T
            For x As Integer = 0 To output.Length - 1
                If ByteArr.Length > x + Index Then
                    output(x) = ByteArr(x + Index)
                End If
            Next
            Return output
        End Function
    End Class
End Module
